package threadPoolTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @CachedThreadPool: 可缓存的线程池  如果线程池的容量超过了任务数，自动回收空闲线程，
 *                          任务增加时可以自动添加新线程，线程池的容量不限制
 * @author: ZBoHang
 * @time: 2021/9/6 17:20
 */
public class CachedThreadPool {
    static ExecutorService cachedExec = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        for (int i = 0; i < 6; i++) {
            final int index = i;
            cachedExec.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " index--> " + index);
                }
            });
        }
    }
}
